package cn.eonshine.cuba.inf.modular.biz.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.eonshine.cuba.inf.common.http.InfStatus;
import cn.eonshine.cuba.inf.modular.biz.entity.TankerGpsDatasheet;
import cn.eonshine.cuba.inf.modular.biz.entity.TankerGpsInf;
import cn.eonshine.cuba.inf.modular.biz.entity.TankerGpsLastport;
import cn.eonshine.cuba.inf.modular.biz.entity.TankerGpsMaster;
import cn.eonshine.cuba.inf.modular.biz.entity.TankerGpsNexttport;
import cn.eonshine.cuba.inf.modular.biz.entity.TankerGpsPosition;
import cn.eonshine.cuba.inf.modular.biz.entity.TankerGpsStatistics;
import cn.eonshine.cuba.inf.modular.biz.entity.TankerGpsVoyage;
import cn.eonshine.cuba.inf.modular.biz.mapper.TankerGpsMasterMapper;
import cn.eonshine.cuba.inf.modular.biz.service.ITankerGpsMasterService;
import cn.eonshine.cuba.inf.modular.common.service.AbstractCommonService;
import cn.hutool.json.JSONUtil;

/**
 * <p>
 * 主Tanker GPS 服务实现类
 * </p>
 *
 * @author Alan
 * @since 2018-07-28
 */
@Service
@Transactional
public class TankerGpsMasterServiceImpl extends AbstractCommonService<TankerGpsMasterMapper, TankerGpsMaster> implements ITankerGpsMasterService {
	@Autowired
	private TankerGpsLastportServiceImpl tankerGpsLastportServiceImpl;
	@Autowired
	private TankerGpsDatasheetServiceImpl tankerGpsDatasheetServiceImpl;
	@Autowired
	private TankerGpsNexttportServiceImpl tankerGpsNexttportServiceImpl;
	@Autowired
	private TankerGpsPositionServiceImpl tankerGpsPositionServiceImpl;
	@Autowired
	private TankerGpsStatisticsServiceImpl tankerGpsStatisticsServiceImpl;
	@Autowired
	private TankerGpsVoyageServiceImpl tankerGpsVoyageServiceImpl;
	
	/**   
	 * <p>Title: messageUpdateHandle</p>   
	 * <p>Description: </p>   
	 * @param clientMsg
	 * @param count
	 * @throws Exception   
	 * @see cn.eonshine.cuba.inf.modular.common.service.AbstractCommonService#messageUpdateHandle(java.lang.String, int)   
	 * @author: Alan
	 * @date:   2019-03-25 22:24:32
	 */
	@Override
	public int messageUpdateHandle(String clientMsg, int count) throws Exception {
		List<TankerGpsInf> tankerGpsInfs = JSONUtil.toList(JSONUtil.parseArray(clientMsg), TankerGpsInf.class);
		for(TankerGpsInf tankerGpsInf : tankerGpsInfs) {
			BigDecimal vesselId=tankerGpsInf.getVesselId();
			boolean isDelete=deleteAllData(vesselId);
			if(isDelete) {
				count ++;
			}
		}
		return count;
	}

	/**   
	 * <p>Title: transportDataHandle</p>   
	 * <p>Description: </p>   
	 * @param obj
	 * @param count
	 * @see cn.eonshine.cuba.inf.modular.common.service.AbstractCommonService#transportDataHandle(java.lang.Object, int)   
	 * @author: Alan
	 * @date:   2019-03-25 22:12:23
	 */
	@Override
	public int transportDataHandle(TankerGpsMaster obj, int count) throws Exception {
		return 0;
	}

	/**   
	 * <p>Title: mappingHandle</p>   
	 * <p>Description: </p>   
	 * @param obj
	 * @throws Exception   
	 * @see cn.eonshine.cuba.inf.modular.common.service.AbstractCommonService#mappingHandle(java.lang.Object)   
	 * @author: Alan
	 * @date:   2019-03-25 22:12:23
	 */
	@Override
	public void mappingHandle(TankerGpsMaster obj) throws Exception {
		
	}

	/**   
	 * <p>Title: isUseCutomerSelectSQL</p>   
	 * <p>Description: </p>   
	 * @see cn.eonshine.cuba.inf.modular.common.service.AbstractCommonService#isUseCutomerSelectSQL()   
	 * @author: Alan
	 * @date:   2019-03-25 22:12:23
	 */
	@Override
	public boolean isUseCutomerSelectSQL() {
		return true;
	}
	
	@Override
	public boolean deleteAllData(BigDecimal vesselId) {
		boolean isDelete=false;
		try {
			if(this.isUpdate == 0) {
				QueryWrapper<TankerGpsLastport> entity = new QueryWrapper<TankerGpsLastport>();
				entity.eq("VESSEL_ID", vesselId);
				isDelete =tankerGpsLastportServiceImpl.remove(entity);
				QueryWrapper<TankerGpsDatasheet> entityOne = new QueryWrapper<TankerGpsDatasheet>();
				entityOne.eq("VESSEL_ID", vesselId);
				isDelete =tankerGpsDatasheetServiceImpl.remove(entityOne);
				QueryWrapper<TankerGpsNexttport> entityTwo = new QueryWrapper<TankerGpsNexttport>();
				entityTwo.eq("VESSEL_ID", vesselId);
				isDelete =tankerGpsNexttportServiceImpl.remove(entityTwo);
				QueryWrapper<TankerGpsPosition> entityThree = new QueryWrapper<TankerGpsPosition>();
				entityThree.eq("VESSEL_ID", vesselId);
				isDelete =tankerGpsPositionServiceImpl.remove(entityThree);
				QueryWrapper<TankerGpsStatistics> entityfour = new QueryWrapper<TankerGpsStatistics>();
				entityfour.eq("VESSEL_ID", vesselId);
				isDelete =tankerGpsStatisticsServiceImpl.remove(entityfour);
				QueryWrapper<TankerGpsVoyage> entityfive = new QueryWrapper<TankerGpsVoyage>();
				entityfive.eq("VESSEL_ID", vesselId);
				isDelete =tankerGpsVoyageServiceImpl.remove(entityfive);
				QueryWrapper<TankerGpsMaster> entitysix = new QueryWrapper<TankerGpsMaster>();
				entitysix.eq("VESSEL_ID", vesselId);
				isDelete =remove(entitysix);
			} else {
				QueryWrapper<TankerGpsMaster> entity = new QueryWrapper<TankerGpsMaster>();
				entity.eq("VESSEL_ID", vesselId);
				TankerGpsMaster bean = new TankerGpsMaster();
				bean.setVesselId(vesselId);
				bean.setLsmsUpdate(new BigDecimal(InfStatus.LSMS_UPDATE_EXTRACT_YES));
				isDelete =update(bean, entity);
			}
			return isDelete ;
		} catch (Exception e) {
			e.printStackTrace();
			isDelete =false;
			return isDelete ;
		}
	}

	/**   
	 * <p>Title: messageUpdateFailHandle</p>   
	 * <p>Description: </p>   
	 * @param clientMsg
	 * @param count
	 * @see cn.eonshine.cuba.inf.modular.common.service.AbstractCommonService#messageUpdateFailHandle(java.lang.String, int)   
	 * @author: Alan
	 * @date:   2019-05-15 11:51:32
	 */
	@Override
	public int messageUpdateFailHandle(String clientMsg, int count) throws Exception {
		return messageUpdateHandle(clientMsg, count);
	}

	
}
